﻿namespace Hidistro.UI.SaleSystem.CodeBehind
{
    using Hidistro.Entities.Members;
    using Hidistro.Entities.Orders;
    using Hidistro.Membership.Context;
    using Hidistro.SaleSystem.Member;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [ParseChildren(true)]
    public class VMemberCenter : WAPMemberTemplatedWebControl
    {
        private Literal litExpenditure;
        private Literal litMemberGrade;
        private Literal litPoints;
        private Literal litUserName;
        private Literal litWaitForPayCount;
        private Literal litWaitForRecieveCount;

        protected override void AttachChildControls()
        {
            PageTitle.AddSiteNameTitle("会员中心");
            Member user = HiContext.Current.User as Member;
            if (user != null)
            {
                this.litUserName = (Literal) this.FindControl("litUserName");
                this.litExpenditure = (Literal) this.FindControl("litExpenditure");
                this.litExpenditure.SetWhenIsNotNull(user.Expenditure.ToString("F0"));
                this.litPoints = (Literal) this.FindControl("litPoints");
                this.litPoints.SetWhenIsNotNull(user.Points.ToString("F0"));
                this.litMemberGrade = (Literal) this.FindControl("litMemberGrade");
                MemberGradeInfo memberGrade = MemberProcessor.GetMemberGrade(user.GradeId);
                if (memberGrade != null)
                {
                    this.litMemberGrade.SetWhenIsNotNull(memberGrade.Name);
                }
                this.litUserName.Text = string.IsNullOrEmpty(user.RealName) ? user.Username : user.RealName;
                this.litWaitForRecieveCount = (Literal) this.FindControl("litWaitForRecieveCount");
                this.litWaitForPayCount = (Literal) this.FindControl("litWaitForPayCount");
                OrderQuery query = new OrderQuery {
                    Status = OrderStatus.WaitBuyerPay
                };
                int userOrderCount = MemberProcessor.GetUserOrderCount(HiContext.Current.User.UserId, query);
                this.litWaitForPayCount.SetWhenIsNotNull(userOrderCount.ToString());
                query.Status = OrderStatus.SellerAlreadySent;
                this.litWaitForRecieveCount.SetWhenIsNotNull(MemberProcessor.GetUserOrderCount(HiContext.Current.User.UserId, query).ToString());
            }
        }

        protected override void OnInit(EventArgs e)
        {
            if (this.SkinName == null)
            {
                this.SkinName = "Skin-VMemberCenter.html";
            }
            base.OnInit(e);
        }
    }
}

